#!/bin/bash
. ./cmd.sh
. ./path.sh

stage=1
#dir=decode_data
dir=exp_pdnn/warm_str_dnn_fmllr_new
base_dnn=exp_pdnn/dnn6_fmllr_new

#ivector directories
train_ivec=exp/fmllr_ivectors_train_128_25
test_ivec=exp/fmllr_ivectors_test_128_25
dev_ivec=exp/fmllr_ivectors_dev_128_25

if [ $stage -le 0 ]; then
echo " extract features in to the working directory"

cp -r $base_dnn/data $dir 
mkdir -p $dir/data

#ivector handling
copy-vector --binary=false ark:$train_ivec/spk_ivector.ark ark,t:$train_ivec/text_spk_ivector.ark
copy-vector --binary=false ark:$test_ivec/spk_ivector.ark ark,t:$test_ivec/text_spk_ivector.ark
copy-vector --binary=false ark:$dev_ivec/spk_ivector.ark ark,t:$dev_ivec/text_spk_ivector.ark

fi

echo " stage 0 is completed "

if [ $stage -le 1 ]; then
   echo " data preparation for unsupervised adaptation "

   for set in test; do
   echo " creating files for dataset $set "
   #for set in train; do
   #  for x in MZMB0;do
     for x in `cat data/$set/spk2gender | awk '{print $1}' | sed ':a;N;$!ba;s/\n/ /g'`; do
       working_dir=$dir/data/$set/$x
       base_dir=$dir/data/$set
       mkdir -p $working_dir

       grep $x $base_dir/cmvn.scp > $working_dir/cmvn.scp
       grep $x $base_dir/feats.scp > $working_dir/feats.scp
       grep $x $base_dir/spk2gender > $working_dir/spk2gender
       grep $x $base_dir/spk2utt > $working_dir/spk2utt
       grep $x $base_dir/utt2spk > $working_dir/utt2spk
       grep $x $base_dir/wav.scp > $working_dir/wav.scp

       cp $base_dir/glm $working_dir/
      
       head -n 3 $base_dir/stm > temp1.stm
       grep $x $base_dir/stm > temp2.stm
       cat temp1.stm temp2.stm > $working_dir/stm
       

      grep --no-filename $x $dir/decode_test/log/decode.* | awk '{print>NR%2}'
      rm 0
      mv 1 $working_dir/text
      #grep $x $base_dir/text > $working_dir/text

       utils/fix_data_dir.sh $working_dir

       grep $x $test_ivec/text_spk_ivector.ark | tr -d '[' | tr -d ']' | cut -d" " -f 2- | sed -e 's/^[ \t]*//' > $working_dir/ivector

       echo " getting the alignments for $x "
       if [ ! -d exp_pdnn/${x}_ali ]; then
         steps_pdnn/align.sh --nj 1 $working_dir data/lang $base_dnn exp_pdnn/${x}_ali
       fi

       echo "creating pfiles for $x"
       if [ ! -f $working_dir/train.pfile.done ]; then
         steps_pdnn/build_nnet_pfile_ivec_fmllr_extract.sh --cmd "$train_cmd" --every-nth-frame 1 --norm-vars true --feature_transform $dir/feature_transform --do-split true --cv-ratio 0.20 --splice-opts "--left-context=5 --right-context=5" --is-spk-mode true --pfile_unit_size 1 --input-dim 465 $working_dir exp_pdnn/${x}_ali $test_ivec $working_dir
         ( cd $working_dir; rm concat.pfile; )
         touch $working_dir/train.pfile.done
       fi
     done
   done
fi


if [ $stage -le 2 ]; then
   echo " data preparation for unsupervised adaptation for dev set"

   for set in dev; do
   echo " creating files for dataset $set "
     for x in `cat data/$set/spk2gender | awk '{print $1}' | sed ':a;N;$!ba;s/\n/ /g'`; do
       working_dir=$dir/data/$set/$x
       base_dir=$dir/data/$set
       mkdir -p $working_dir

       grep $x $base_dir/cmvn.scp > $working_dir/cmvn.scp
       grep $x $base_dir/feats.scp > $working_dir/feats.scp
       grep $x $base_dir/spk2gender > $working_dir/spk2gender
       grep $x $base_dir/spk2utt > $working_dir/spk2utt
       grep $x $base_dir/utt2spk > $working_dir/utt2spk
       grep $x $base_dir/wav.scp > $working_dir/wav.scp

       cp $base_dir/glm $working_dir/

       head -n 3 $base_dir/stm > temp1.stm
       grep $x $base_dir/stm > temp2.stm
       cat temp1.stm temp2.stm > $working_dir/stm


      grep --no-filename $x $dir/decode_dev/log/decode.* | awk '{print>NR%2}'
      rm 0
      mv 1 $working_dir/text
      #grep $x $base_dir/text > $working_dir/text

       utils/fix_data_dir.sh $working_dir

       grep $x $dev_ivec/text_spk_ivector.ark | tr -d '[' | tr -d ']' | cut -d" " -f 2- | sed -e 's/^[ \t]*//' > $working_dir/ivector

       echo " getting the alignments for $x "
       if [ ! -d exp_pdnn/${x}_ali ]; then
         steps_pdnn/align.sh --nj 1 $working_dir data/lang $base_dnn exp_pdnn/${x}_ali
       fi

       echo "creating pfiles for $x"
       if [ ! -f $working_dir/train.pfile.done ]; then
         steps_pdnn/build_nnet_pfile_ivec_fmllr_extract.sh --cmd "$train_cmd" --every-nth-frame 1 --norm-vars true --feature_transform $dir/feature_transform --do-split true --cv-ratio 0.20 --splice-opts "--left-context=5 --right-context=5" --is-spk-mode true --pfile_unit_size 1 --input-dim 465 $working_dir exp_pdnn/${x}_ali $dev_ivec $working_dir
         ( cd $working_dir; rm concat.pfile; )
         touch $working_dir/train.pfile.done
       fi
     done
   done
fi
